<!-- user表单弹窗 -->
<form id="user-form" lay-filter="user-form" class="layui-form model-form">
    <% if(!overstep) { %>
    <input name="userId" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">帐号</label>
        <div class="layui-input-block">
            <% if(roleId == 5 &&  edit== 0) { %>
            <input name="username" placeholder="请输入帐号" type="text" class="layui-input" maxlength="20"
                   lay-verify="required|username" required value="${userName}" disabled/>
            <% } %>
            <% if(roleId == 5 &&  edit== 1) { %>
            <input name="username" placeholder="请输入帐号" type="text" class="layui-input" maxlength="20"
                   lay-verify="required|username" required disabled/>
            <% } %>
            <% if(roleId != 5 ) { %>
            <input name="username" placeholder="请输入帐号" type="text" class="layui-input" maxlength="20"
                   lay-verify="required|username" required/>
            <% } %>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input name="nickName" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input name="phone" placeholder="请输入手机号" type="text" class="layui-input" lay-verify="required|phone"
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" checked/>
            <input type="radio" name="sex" value="女" title="女"/>
        </div>
    </div>
    <% if(edit == 0) {%>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <select id="rolesaaa" name="roleId" lay-verify="required">
                <% if(roleId == 5) { %>
                <option value="3" selected>店员</option>
                <% } %>
                <% if(roleId == 1) { %>
                	<option value="">请选择</option>
	                <% for(role in roles) { %>
	                <option value="${role.roleId}">${role.roleName}</option>
	                <% } %>
                <% } %>
                <% if(roleId == 2) { %>
                	<!--<option value="">请选择</option>-->
	                <!--<option value="4">招商</option>-->
                <option value="">请选择</option>
                <% for(role in roles) { %>
                <option value="${role.roleId}">${role.roleName}</option>
                <% } %>
                <% } %>
            </select>
        </div>
    </div>
    <% } %>
    <% } %>
    <% if(overstep) { %>
    <div class="layui-form-item model-form-footer">
        每个门店最多录入五条店员信息，您已达到限制数目！
    </div>
    <% } %>
    <% if(!overstep) { %>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="user-form-submit" lay-submit>保存</button>
    </div>
    <% } %>

</form>

<script>
    layui.use(['layer', 'admin', 'form', 'formSelects'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;

        form.render('radio');
        form.render('select');

        var url = 'system/user/add';
        // 回显user数据
        var user = admin.getTempData('t_user');
        if (user) {
            $('input[name="username"]').attr('readonly', 'readonly');
            url = 'system/user/update';

            // // 遍历select
            // $("#rolesaaa").each(function () {
            //     // this代表的是<option></option>，对option再进行遍历
            //     $(this).children("option").each(function () {
            //         // 判断需要对那个选项进行回显
            //         if (this.value == user.roles[0].roleId) {
            //             // 进行回显
            //             $(this).attr("selected", "selected");
            //         }
            //     });
            // });

            form.val('user-form', user);
            form.render('select');
        }
        //要放在form.on外面，千万不能放在提交步骤中，否则会不触发
        form.verify({
            username: [
                /^\w*$/
                , '请填入数字、字母或者下划线'
            ]

            //我们既支持上述函数式的方式，也支持下述数组的形式
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
        });

        // 表单提交事件
        form.on('submit(user-form-submit)', function (data) {
            layer.load(2);
            data.field.shopId = $("#shopInfo").attr("data");
            $.post(url, data.field, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {
                        icon: 1
                    });
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {
                        icon: 2
                    });
                }
            });
            return false;
        });
    });
</script>